package cn.com.jslh.boarpro.main.view.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.com.jslh.boarpro.R;
import cn.com.jslh.boarpro.base.BaseFragment;
import cn.com.jslh.boarpro.base.mvp.BaseListView;
import cn.com.jslh.boarpro.main.presenter.InfoShowPresenterImpl;
import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;

/**
 * 作者：周洲 on 2017/7/31 08:33
 * <p>
 * 邮箱：double0zhou@126.com
 */

public class InfoShowFragment extends BaseFragment implements BaseListView {
    @BindView(R.id.inforecycleview)
    RecyclerView inforecycleview;

    public static String MuZhuType = "MuZhuType"; //母猪类型
    public static String GongZhuType = "GongZhuType";//公猪类型
    public static String FenMianType = "FenMianType";//分娩母猪
    public static String DuanNaiType = "DuanNaiType";//断奶母猪
    public static String Clear = "Clear"; //清空显示界面
    private InfoShowPresenterImpl infoShowPresenter;

    @Override
    public void onStart() {
        super.onStart();
        if (!EventBus.getDefault().isRegistered(this))
            EventBus.getDefault().register(this);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.info_show_frgment_layout, container, false);
        unbinder = ButterKnife.bind(this, view);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false);
        inforecycleview.setLayoutManager(linearLayoutManager);
        inforecycleview.addItemDecoration(new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL));
        infoShowPresenter = new InfoShowPresenterImpl(getContext(), this);
        infoShowPresenter.setUI(inforecycleview);
        return view;
    }

    @Override
    public void onItemClicked(String... params) {

    }

    // This method will be called when a SomeOtherEvent is posted
    @Subscribe
    public void handleSomethingElse(InfoEvent event) {
        infoShowPresenter.upAsyncDatas(event.netSuccessListener, event.params);
    }

    @Override
    public void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    public static class InfoEvent {
        String[] params;
        getInfoSuccessListener netSuccessListener;

        public InfoEvent(getInfoSuccessListener infoSuccessListener,String... params) {
            this.params = params;
            this.netSuccessListener = infoSuccessListener;
        }

    }


    /**
     * 根据猪耳号和类型获取猪信息
     *
     * @param infoSuccessListener
     */
    public static void getPigInfo(getInfoSuccessListener infoSuccessListener,String... params ) {
        EventBus.getDefault().post(new InfoShowFragment.InfoEvent(infoSuccessListener,params));
    }
    public interface getInfoSuccessListener {
        public void success(String s,String id);
    }

    public static void initInfoShowFragment(FragmentManager fm){
        FragmentTransaction tx = fm.beginTransaction();
        tx.add(R.id.fragment_layout, new InfoShowFragment());
        tx.commit();
    }
}
